package hadlers

import (
	"car-trade-pro/cache"
	"car-trade-pro/modle"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func RepoAdd(c *gin.Context) {

	repCode := c.PostForm("repCode")
	repoName := c.PostForm("repoName")

	var repo = modle.Repo{
		RepCode:  repCode,
		RepoName: repoName,
	}

	modle.DB.Debug().Create(&repo)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": repo,
	})
	return

}

func RepoOrderAdd(c *gin.Context) {
	repId, _ := strconv.Atoi(c.PostForm("repId"))
	orderCode := c.PostForm("orderCode")
	goodNum, _ := strconv.Atoi(c.PostForm("goodNum"))
	orderPrice, _ := strconv.Atoi(c.PostForm("orderPrice"))

	var repoOrderAdd = modle.RepoOrder{
		RepId:      repId,
		OrderCode:  orderCode,
		GoodNum:    goodNum,
		OrderPrice: orderPrice,
	}

	modle.DB.Debug().Create(&repoOrderAdd)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": repoOrderAdd,
	})
	return
}

func RepoOrderList(c *gin.Context) {
	/*

		SELECT
		   repos.rep_code AS RepCode,
		   repos.repo_name AS RepoName,
		   repo_orders.order_code AS OrderCode,
		   repo_orders.order_price AS OrderPrice,
		   repo_orders.id AS OrderId,
		   repo_orders.good_num AS GoodNum
		 FROM
		repo_orders
		LEFT JOIN  repos on repos.id=repo_orders.rep_id

	*/

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size

	var data []modle.RepoOrderList
	redis_key := "Repo_order_list_page" + strconv.Itoa(page)
	cacheData, _ := cache.Rab.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		modle.DB.Table("repo_orders").
			Select(" repos.rep_code AS RepCode",
				"   repos.repo_name AS RepoName",
				" repo_orders.order_code AS OrderCode",
				" repo_orders.order_price AS OrderPrice",
				"  repo_orders.id AS OrderId",
				" repo_orders.good_num AS GoodNum",
			).
			Joins("LEFT JOIN  repos on repos.id=repo_orders.rep_id").
			Debug().
			Offset(offset).
			Limit(size).
			Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rab.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	var total int64
	modle.DB.Model(&modle.RepoOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  data,
		"total": total,
	})
	return

}

func RepoOrderDetail(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))

	var data []modle.RepoOrderList
	redis_key := "Repo_order_Detail_id" + strconv.Itoa(id)
	cacheData, _ := cache.Rab.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		modle.DB.Table("repo_orders").
			Select(" repos.rep_code AS RepCode",
				"   repos.repo_name AS RepoName",
				" repo_orders.order_code AS OrderCode",
				" repo_orders.order_price AS OrderPrice",
				"  repo_orders.id AS OrderId",
				" repo_orders.good_num AS GoodNum",
			).
			Joins("LEFT JOIN  repos on repos.id=repo_orders.rep_id").
			Debug().
			Where("repo_orders.id=?", id).
			Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rab.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	var total int64
	modle.DB.Model(&modle.RepoOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return

}
